﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;

namespace DTC.Converters
{
    [ValueConversion(typeof(double), typeof(string))]
    class SiStringToDoubleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            return ConvertTo(value);
        }
        public object ConvertBack(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            return ConvertFrom(value);
        }
        public object ConvertTo(object value)
        {
            if(value == null)
                return "";
            if(!(value is double))
                return "";
            double val = (double)value;
            return DTC.Database.DTCIssueForm.ValSignParser(val);
        }
        public object ConvertFrom(object value)
        {
            if (value == null)
                return 0;
            if (!(value is string))
                return 0;
            string str = (string) value;
            string val = "";
            string acr = "";
            double multiplier = 1.0;
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsDigit(str[i]))
                    val += str[i];
                if (char.IsLetter(str[i]))
                    acr += str[i];
            }
            multiplier = DTC.Database.DTCIssueForm.SiParser(acr);
            double dval;
            double.TryParse(val, out dval);
            return dval * multiplier;            

        }
    }
}
